package com.fan.springboot.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fan.springboot.domain.News;
import com.fan.springboot.service.GlobalParams;
import com.fan.springboot.service.NewsService;

@RestController
@RequestMapping("/api")
public class NewsController {

	@Autowired
	private NewsService newsService;

	// 按时间倒叙获取项目列表
	@RequestMapping(value = "/getNewsList")
	public Object getNewsList(@RequestParam(value = "currentPage") int currentPage,
			@RequestParam(value = "limit") int limit) {

		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("data", newsService.findAllbyDESC(currentPage, limit));
		resultMap.put(GlobalParams.SUCCESS, true);

		return resultMap;

	}

	// 添加或修改项目
	@RequestMapping(value = "/addOrUpdateNews", method = RequestMethod.POST)
	public Object addOrUpdateNews(@RequestBody News news) {
		if (news.getId() != "" && news.getId() != null) {
			newsService.update(news);
		} else {
			newsService.save(news);
		}

		Map<String, Object> map = new HashMap<>();
		map.put(GlobalParams.SUCCESS, true);
		return map;
	}

	// 删除项目
	@RequestMapping(value = "/deleteNews", method = RequestMethod.POST)
	public Object deleteNews(String id) {

		Map<String, Object> resultMap = new HashMap<>();
		newsService.delete(id);
		resultMap.put(GlobalParams.SUCCESS, true);

		return resultMap;

	}

	// 根据id获取新闻详细
	@RequestMapping(value = "/getNewsDetail")
	public Object getNewsDetail(String id) {

		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("data", newsService.getNewsById(id));
		resultMap.put(GlobalParams.SUCCESS, true);

		return resultMap;
	}
	
	

}
